import { http } from "@/utils/http";
// import { getToken } from "@/utils/auth";
import { getToken175, getToken43 } from "@/utils/token";

type Result = {
  message: string;
  data: any;
  code: number;
};

// 为task API添加Authorization头的辅助函数
const taskRequest = <T>(method: string, url: string, data?: object) => {
  const token = getToken175(); // 每次调用都获取最新的token
  return http.request<T>(method as any, url, {
    data,
    headers: {
      Authorization: token ? `Bearer ${token}` : undefined,
    },
  });
};

const remoteRequest = <T>(method: string, url: string, data?: object) => {
  const remoteToken = getToken43(); // 每次调用都获取最新的token
  return http.request<T>(method as any, url, {
    data,
    headers: {
      Authorization: remoteToken ? `Bearer ${remoteToken}` : undefined,
    },
  });
};

/** 获取资产列表 */
export const getAssetList = ({
  data,
  type = "asset",
}: {
  data: object;
  type: string;
}) => {
  return taskRequest<Result>("post", `/api/api/${type}/data`, data);
};

/** 获取资产详情 */
export const getAssetDetail = (data?: object) => {
  return taskRequest<Result>("post", "/api/api/asset/detail", data);
};

/** 获取资产变更日志 */
export const getAssetChangelog = (data?: object) => {
  return taskRequest<Result>("post", "/api/api/asset/changelog", data);
};

/** 删除资产 */
export const deleteAsset = (data?: object) => {
  return taskRequest<Result>("post", "/api/api/data/delete", data);
};

/** 添加标签 */
export const addTag = (data?: object) => {
  return taskRequest<Result>("post", "/api/api/data/add/tag", data);
};

/** 删除标签 */
export const deleteTag = (data?: object) => {
  return taskRequest<Result>("post", "/api/api/data/delete/tag", data);
};

/** 获取资产总数 */
export const getAssetTotal = (data?: object) => {
  return taskRequest<Result>("post", "/api/api/data/total", data);
};

/** 获取异地资产列表 */
export const getRemoteAssetList = (data?: object) => {
  return remoteRequest<Result>("post", "/api/api2/asset/data", data);
};

/** 获取异地资产总数 */
export const getRemoteAssetTotal = (data?: object) => {
  return remoteRequest<Result>("post", "/api/api2/data/total", data);
};

/** 获取异地资产详情 */
export const getRemoteAssetDetail = (data?: object) => {
  return remoteRequest<Result>("post", "/api/api2/asset/detail", data);
};

/** 删除异地资产 */
export const deleteRemoteAsset = (data?: object) => {
  return remoteRequest<Result>("post", "/api/api2/data/delete", data);
};

/** 添加异地标签 */
export const addRemoteTag = (data?: object) => {
  return remoteRequest<Result>("post", "/api/api2/data/add/tag", data);
};

/** 删除异地标签 */
export const deleteRemoteTag = (data?: object) => {
  return remoteRequest<Result>("post", "/api/api2/data/delete/tag", data);
};

/** 获取敏感信息结果 */
export const getSensitiveResult = (data?: object) => {
  return taskRequest<Result>("post", "/api/api/sensitive/result/data2", data);
};

/** 获取异地资产变更日志 */
export const getRemoteAssetChangelog = (data?: object) => {
  return remoteRequest<Result>("post", "/api/api2/asset/changelog", data);
};

/** 更新资产状态 */
export const updateAssetStatus = (data?: object) => {
  return taskRequest<Result>("post", "/api/api/data/update/status", data);
};

// ============ Gadget链管理 API ============

/** 上传JAR包 */
export const uploadGadget = (formData: FormData) => {
  const token = getToken175();
  return http.request<Result>("post", "/api/gadget/upload", {
    data: formData,
    headers: {
      "Content-Type": "multipart/form-data",
      Authorization: token ? `Bearer ${token}` : undefined,
    },
  });
};

/** 手动添加Gadget */
export const addGadget = (data?: {
  chain_name: string;
  gadget_type: string;
  component_name?: string;
  component_version?: string;
  entry_class?: string;
  chain_classes?: string;
  can_exec_command?: boolean;
  can_jndi_inject?: boolean;
  can_dns_query?: boolean;
  can_file_read?: boolean;
  can_file_write?: boolean;
  can_http_request?: boolean;
  jdk_version?: string;
  os_support?: string;
  dependencies?: string;
  cve_list?: string;
  description?: string;
  risk_level?: string;
  tags?: string;
}) => {
  return taskRequest<Result>("post", "/api/gadget/add", data);
};

/** 查询Gadget列表 */
export const getGadgetList = (data?: {
  chain_name?: string;
  gadget_type?: string;
  can_exec_command?: boolean;
  can_jndi_inject?: boolean;
  is_available?: boolean;
  page?: number;
  per_page?: number;
}) => {
  return taskRequest<Result>("post", "/api/gadget/list", data);
};

/** 获取Gadget详情 */
export const getGadgetDetail = (data?: { gadget_id: number }) => {
  return taskRequest<Result>("post", "/api/gadget/detail", data);
};

/** 更新Gadget配置 */
export const updateGadget = (data?: {
  gadget_id: number;
  chain_name?: string;
  gadget_type?: string;
  component_name?: string;
  component_version?: string;
  entry_class?: string;
  chain_classes?: string;
  can_exec_command?: boolean;
  can_jndi_inject?: boolean;
  can_dns_query?: boolean;
  can_file_read?: boolean;
  can_file_write?: boolean;
  can_http_request?: boolean;
  jdk_version?: string;
  os_support?: string;
  dependencies?: string;
  risk_level?: string;
  cve_list?: string;
  description?: string;
  is_available?: boolean;
  test_status?: string;
  test_result?: string;
}) => {
  return taskRequest<Result>("post", "/api/gadget/update", data);
};

/** 启用/禁用Gadget */
export const toggleGadget = (data?: {
  gadget_id: number;
  is_available: boolean;
}) => {
  return taskRequest<Result>("post", "/api/gadget/toggle", data);
};

/** 删除Gadget */
export const deleteGadget = (data?: {
  gadget_ids: number[];
  delete_file?: boolean;
}) => {
  return taskRequest<Result>("post", "/api/gadget/delete", data);
};

// ============ Payload生成 API ============

/** 生成Payload */
export const generatePayload = (data?: {
  chain_name?: string;
  gadget_id?: number;
  payload_type: string;
  command: string;
  encoding?: string;
  target_ip?: string;
  target_url?: string;
  scan_task?: string;
}) => {
  return taskRequest<Result>(
    "post",
    "/api/gadget_chain/generate_payload",
    data
  );
};

/** 获取可用链列表 */
export const getAvailableChains = () => {
  const token = getToken175();
  return http.request<Result>("get", "/api/gadget_chain/get_available_chains", {
    headers: {
      Authorization: token ? `Bearer ${token}` : undefined,
    },
  });
};

/** 查询Payload生成日志 */
export const getPayloadGenerationLogs = (data?: {
  chain_name?: string;
  scan_task?: string;
  payload_type?: string;
  status?: string;
  page?: number;
  per_page?: number;
}) => {
  return taskRequest<Result>(
    "post",
    "/api/api/gadget_chain/generation_logs",
    data
  );
};

// ============ Java漏洞检测模块 API ============

/** 查询Java漏洞库 */
export const queryJavaVulnerabilities = (data?: {
  keyword?: string;
  tags?: string;
  cve_id?: string;
  severity?: string;
  min_cvss?: number;
  max_cvss?: number;
  page?: number;
  per_page?: number;
}) => {
  return taskRequest<Result>(
    "post",
    "/api/asset/query_java_vulnerabilities",
    data
  );
};

/** 单个漏洞扫描 */
export const scanVulnerability = (data?: {
  vulnerability_id: number;
  target_url: string;
}) => {
  return taskRequest<Result>("post", "/api/asset/vulnerability/scan", data);
};

/** 批量漏洞扫描 */
export const batchScanVulnerability = (data?: {
  vulnerability_ids: number[];
  target_url: string;
}) => {
  return taskRequest<Result>(
    "post",
    "/api/asset/vulnerability/batch_scan",
    data
  );
};

/** 查询扫描任务状态 */
export const getScanStatus = (taskId: string) => {
  return taskRequest<Result>(
    "post",
    `/api/asset/vulnerability/scan_status/${taskId}`
  );
};

/** 获取扫描结果 */
export const getScanResult = (
  taskId: string,
  data?: { page?: number; per_page?: number }
) => {
  return taskRequest<Result>(
    "post",
    `/api/asset/vulnerability/scan_result/${taskId}`,
    data
  );
};

/** 查询扫描历史 */
export const getScanHistory = (data?: {
  status?: string;
  scan_type?: string;
  page?: number;
  per_page?: number;
}) => {
  return taskRequest<Result>(
    "post",
    "/api/asset/vulnerability/scan_history",
    data
  );
};

// ============ 内存马管理模块 API ============

/** 上传内存马文件 */
export const uploadMemshell = (formData: FormData) => {
  const token = getToken175();
  return http.request<Result>("post", "/api/memshell/upload", {
    data: formData,
    headers: {
      "Content-Type": "multipart/form-data",
      Authorization: token ? `Bearer ${token}` : undefined,
    },
  });
};

/** 手动添加内存马 */
export const addMemshell = (data?: {
  shell_name: string;
  shell_type?: string;
  description?: string;
  java_code?: string;
  target_middleware?: string;
  target_framework?: string;
  jdk_version?: string;
  has_cmd_shell?: boolean;
  has_file_upload?: boolean;
  injection_method?: string;
  risk_level?: string;
  tags?: string;
  is_template?: boolean;
  author?: string;
}) => {
  return taskRequest<Result>("post", "/api/memshell/add", data);
};

/** 获取内存马列表 */
export const getMemshellList = (data?: {
  page?: number;
  page_size?: number;
  shell_type?: string;
  target_middleware?: string;
  risk_level?: string;
  is_available?: string;
  is_template?: string;
  search?: string;
}) => {
  return taskRequest<Result>("post", "/api/memshell/list", data);
};

/** 获取内存马详情 */
export const getMemshellDetail = (data?: { shell_id: number }) => {
  return taskRequest<Result>("post", "/api/memshell/detail", data);
};

/** 更新内存马信息 */
export const updateMemshell = (data?: {
  shell_id: number;
  shell_name?: string;
  description?: string;
  java_code?: string;
  risk_level?: string;
  tags?: string;
  is_available?: boolean;
}) => {
  return taskRequest<Result>("post", "/api/memshell/update", data);
};

/** 切换内存马状态 */
export const toggleMemshell = (data?: { shell_id: number }) => {
  return taskRequest<Result>("post", "/api/memshell/toggle", data);
};

/** 删除内存马 */
export const deleteMemshell = (data?: { shell_id: number }) => {
  return taskRequest<Result>("post", "/api/memshell/delete", data);
};

/** 生成定制化内存马 */
export const generateMemshell = (data?: {
  shell_id: number;
  password?: string;
  callback_url?: string;
  callback_port?: string;
}) => {
  return taskRequest<Result>("post", "/api/memshell/generate", data);
};

/** 导出内存马数据 */
export const exportMemshell = (data?: { shell_type?: string }) => {
  return taskRequest<Result>("post", "/api/memshell/export", data);
};

// ============ Gadget链查询模块 API ============

/** 查询Gadget链列表 */
export const queryGadgetChain = (data?: {
  library?: string;
  jdk_version?: string;
}) => {
  return taskRequest<Result>("post", "/api/api/asset/gadget/query", data);
};

/** 生成Gadget Payload */
export const generateGadgetPayload = (data?: {
  gadget_id: number;
  command: string;
  encoding?: string;
}) => {
  return taskRequest<Result>("post", "/api/api/asset/gadget/generate", data);
};

/** 获取Gadget链详情 */
export const getGadgetChainDetail = (data?: { gadget_id: number }) => {
  return taskRequest<Result>("post", "/api/api/asset/gadget/detail", data);
};
